#pragma once
#include "random.h"	// for my special random function

#define BOARD_TILE_W	20
#define BOARD_TILE_H	20

class Board
{
public:
	int colorW, colorH;
	Uint32 ** colors;
	Uint32 timer;
	Uint32 changeTime;

	void randomizeColors()
	{
		for(int h = 0; h < colorH; ++h)
		{
			for(int w = 0; w < colorW; ++w)
			{
				colors[h][w] = randomColor();
			}
		}
		char * myImage[6] =
		{
			".-. .-. ",
			"-#- -#- ",
			"-#--.-. ",
			"-###-#- ",
			"-#-#-#- ",
			"-#-#-#- ",
		};
		Uint32 pound = randomColor(), 
			dash = randomColor(), dot = randomColor();
		for(int r = 0; r < 6; ++r)
		{
			for(int c = 0; c < 8; ++c)
			{
				switch(myImage[r][c])
				{
				case '#':	colors[r][c] = pound;	break;
				case '-':	colors[r][c] = dash;	break;
				case '.':	colors[r][c] = dot;		break;
				}
			}
		}

	}

	Board()
	{
		colorW = 15;
		colorH = 8;
		colors = new Uint32 * [colorH];
		for(int i = 0; i < colorH; ++i)
			colors[i] = new Uint32[colorW];

		randomizeColors();
		changeTime = 5000;
		timer = 0;
	}

	void draw(SDL_Surface * screen)
	{
		SDL_Rect colorCursor = {0,0,15,15};
		for(int h = 0; h < colorH; ++h)
		{
			for(int w = 0; w < colorW; ++w)
			{
				colorCursor.x = w * BOARD_TILE_W;
				colorCursor.y = h * BOARD_TILE_H;
				SDL_FillRect(screen, &colorCursor, colors[h][w]);
			}
		}
	}

	void update(int ms)
	{
		timer += ms;
		if(timer >= changeTime)
		{
			timer -= changeTime;
			randomizeColors();
		}
	}

	Vector whichTileIsPressed(
		const int & a_mouseX, const int & a_mouseY)
	{
		Vector out;
		out.x = (float)a_mouseX/BOARD_TILE_W;
		out.y = (float)a_mouseY/BOARD_TILE_H;
		return out;
	}

	void handleInput(SDL_Event e)
	{
		switch(e.button.button)
		{
		case SDL_BUTTON_LEFT:
			// what am I pressing?
			Vector tile = whichTileIsPressed(e.button.x, e.button.y);
			// is that a valid thing to press?
			if(tile.x >= 0 && tile.x < colorW
			&& tile.y >= 0 && tile.y < colorH)
			{
				// do my action
				colors[(int)tile.y][(int)tile.x] = randomColor();
			}
			break;
		}
	}
};